/**
 * 
 */
package com.study.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Administrator
 * @date 2019年6月9日
 */
public class Demo {
	public static void main(String[] args) throws Exception  {
        //test();
		//test1();
		test2();
    }  
	
	public static void test() throws Exception{
		File file = new File("D:\\logs\\log.log");
        InputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()]; 
        //FileInputStream类提供了一个available()方法，该方法在读取本地文件时，返回文件的总字节数。
        inputStream.read(bytes);
        System.out.println(new String(bytes));
        inputStream.close();
	}
	public static void test1() throws Exception{
		File file = new File("D:\\logs\\log.log");
		InputStream inputStream = new FileInputStream(file);
		byte[] bytes = new byte[1024]; 
		//FileInputStream类提供了一个available()方法，该方法在读取本地文件时，返回文件的总字节数。
		while(inputStream.read(bytes) !=-1){
			System.out.println(new String(bytes));	
			bytes = new byte[1024]; 
		}
		//System.out.println(new String(bytes));	
		
		inputStream.close();
	}
	
	public static void test2() throws IOException{
		File file = new File("data.txt");
        FileOutputStream outputStream = new FileOutputStream(file);
        FileChannel channel = outputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        String string = "java nio";
        buffer.put(string.getBytes());
        buffer.flip();     //此处必须要调用buffer的flip方法
        channel.write(buffer);
        channel.close();
        outputStream.close();
	}
}
